package com.yanqu.road.server.logic.task.condition;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserVehicle;
import com.yanqu.road.entity.task.TaskConditionInfo;
import com.yanqu.road.entity.task.args.VehicleArgs;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.task.BaseCondition;
import com.yanqu.road.server.logic.task.BaseTaskData;
import com.yanqu.road.server.manger.config.VehicleMgr;

import java.math.BigInteger;

/**
 * 珍兽累计升级次数（存在的）
 */
public class ExistVehicleTotalUpgradeTimesCondition extends BaseCondition {

	public ExistVehicleTotalUpgradeTimesCondition(TaskConditionInfo conditionInfo, BaseTaskData quest, BigInteger value, Object initArgs) {
		super(conditionInfo, quest, value, initArgs);
	}

	@Override
	public void addTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.ExistVehicleTotalUpgradeTimes.getValue(), handle);
	}

	@Override
	public void removeTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.ExistVehicleTotalUpgradeTimes.getValue(), handle);
	}

	public boolean isCompleted(GamePlayer player) {
		BigInteger param4 = taskConditionInfo.getParamList().get(3);
		return getValue().compareTo(param4) >= 0;
	}

	@Override
	protected void changeValue(Object object) {
		VehicleArgs args = (VehicleArgs)object;
		int totalTimes = 0;
		if(null != args && null != args.getVehicleMap()){
			for(UserVehicle userVehicle : args.getVehicleMap().values()){
				if(VehicleMgr.isUserVehicleExist(userVehicle)){
					int upgradeTimes = userVehicle.getLevel() - 1;
					totalTimes += upgradeTimes;
				}
			}
		}
		setValue(BigInteger.valueOf(totalTimes));
	}

}
